package mt.client;

import java.util.LinkedList;

import mt.Order;

public abstract class StockResume {
private boolean isBuyOrder;
	


	public StockResume(String stock) {
		this.stock = stock;
		this.isBuyOrder = false;

		setHighestPrice();
		setLowestPrice();
		setMedianPrice();
		setTotalUnits();
	}
	
	public StockResume(){
		
	}

	public boolean isBuyOrder() {
		return isBuyOrder;
	}

	public void setBuyOrder(boolean isBuyOrder) {
		this.isBuyOrder = isBuyOrder;
	}

	@Override
	public String toString() {
		return "Stock: " + stock
				+ " | Quantity: " + totalUnits + " | Median Price: " + medianPrice
				+ " | " + "Lowest Price: "+ lowestPrice + " | Highest Price: "
				+ highestPrice;
	}

	public static class BuyOrdersResume extends StockResume {

		public BuyOrdersResume(String stock) {
			super(stock);
		}
		public BuyOrdersResume(){
			super();
		}

	}

	public static class SellOrdersResume extends StockResume {

		public SellOrdersResume(String stock) {
			super(stock);
		}

	}

	/**
	 * @return the stock
	 */
	public String getStock() {
		return stock;
	}

	/**
	 * @return the ordersList
	 */
	public LinkedList<Order> getOrdersList() {
		return ordersList;
	}

	/**
	 * Adds a order to the list of orders. Also actualizes the variables
	 * medianPrice, lowestPrice, highestPrice e totalUnits
	 * 
	 * @param order
	 */
	public void addOrder(Order order) {
		ordersList.add(order);

		setHighestPrice();
		setLowestPrice();
		setMedianPrice();
		setTotalUnits();
	}

	/**
	 * @return the quantity
	 */
	public int getTotalUnits() {
		return totalUnits;
	}

	/**
	 * Set totalUnits to the sum of the number of orders in the list of orders.
	 */
	private void setTotalUnits() {

		int totalUnits = 0;
		if (!ordersList.isEmpty()) {
			for (Order order : ordersList) {
				totalUnits += order.getNumberOfUnits();
			}
		}

		this.totalUnits = totalUnits;

	}

	
	@SuppressWarnings("unused")
	public void setTotalUnits(int units) {
		
		this.totalUnits = units;

	}

	
	
	
	
	
	/**
	 * @return the mediumPrice
	 */
	public double getMedianPrice() {
		return medianPrice;
	}

	/**
	 * Sets medianPrice variable equal do the median price/unit value of the
	 * orders list
	 */
	private void setMedianPrice() {

		double median = 0;
		if (!ordersList.isEmpty()) {
			for (Order order : ordersList) {
				median += order.getPricePerUnit();
			}
			median /= ordersList.size();
		}

		this.medianPrice = median;
	}

	/**
	 * @return the lowestPrice
	 */
	public double getLowestPrice() {
		return lowestPrice;
	}

	/**
	 * Sets lowestPrice variable equal do the lowest price/unit value on the
	 * orders list
	 */
	private void setLowestPrice() {

		double lowestValue = 0;
		if (!ordersList.isEmpty()) {

			lowestValue = ordersList.getFirst().getPricePerUnit();

			for (Order order : ordersList) {
				if (order.getPricePerUnit() < lowestValue) {
					lowestValue = order.getPricePerUnit();
				}
			}
		}
		this.lowestPrice = lowestValue;
	}

	/**
	 * @return the highestPrice
	 */
	public double getHighestPrice() {
		return highestPrice;
	}

	/**
	 * Sets highestPrice variable equal do the highest price/unit value on the
	 * orders list
	 */
	private void setHighestPrice() {
		double highestValue = 0;
		if (!ordersList.isEmpty()) {

			highestValue = ordersList.getFirst().getPricePerUnit();

			for (Order order : ordersList) {
				if (order.getPricePerUnit() > highestValue) {
					highestValue = order.getPricePerUnit();
				}
			}
		}
		this.highestPrice = highestValue;
	}

	/**
	 * Total quantity of orders
	 */
	private String stock;

	/**
	 * Total quantity of orders
	 */
	private int totalUnits;

	/**
	 * Medium price per unit
	 */
	private double medianPrice;

	/**
	 * Medium price per unit
	 */
	private double lowestPrice;

	/**
	 * Medium price per unit
	 */
	private double highestPrice;

	/**
	 * List of orders
	 */
	private LinkedList<Order> ordersList = new LinkedList<Order>();

}